package com.yd.controller.app;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.yd.pojo.Supplier;
import com.yd.service.SupplierService;
import com.yd.tool.MD5Hash;
import com.yd.tool.UtsReg;
import com.yd.tool.UtsUuid;

@Controller
public class SupplierControllerApp {
	
	@Resource
	private SupplierService supplierService;
	
	//供应商登录
	@RequestMapping(value = "/app/supplier/login")
	@ResponseBody
	public String app_supplier_login(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();
		//获取参数
		String loginname="";
		String loginpass="";
		//验证参数
		if (UtsReg.getIsPhone(request.getParameter("loginname"))){loginname = request.getParameter("loginname").trim();}else{jsonObject.put("msg", "账号格式错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if (UtsReg.getStringPass(request.getParameter("loginpass"))){loginpass = request.getParameter("loginpass").trim();}else{jsonObject.put("msg", "密码格式错误");jsonObject.put("error", "1");return jsonObject.toString();}
		//通过账号查询此条数据
		Supplier supplier = supplierService.selectOne(new EntityWrapper<Supplier>().eq("loginname", loginname));
		
		//判断此账号是否有数据
		if(supplier==null){//如果没有数据 那说明不存在此账号
			jsonObject.put("msg", "账号或者密码错误");jsonObject.put("error", "1");return jsonObject.toString();
		}
		//走到这里说明有账号，那么要判断这个账号的密码是否正确，传过来的密码要加密后再对比
		if(!supplier.getLoginpass().equals(MD5Hash.getMd5HashPasswod(loginpass))){
			jsonObject.put("msg", "账号或者密码错误");jsonObject.put("error", "1");return jsonObject.toString();
		}
		supplier.setPhone(loginname);
		//生成一个随机数当作token
		supplier.setToken(UtsUuid.getUuid());
		//把这个token更新到数据库，每次登陆都要重新生成一次
		if(!supplierService.updateById(supplier)){
			jsonObject.put("msg", "失败");jsonObject.put("error", "1");return jsonObject.toString();
		}
		//把实体类里面的密码清空 然后返回给前台(所有返回用户信息的时候都要清空重要的信息，比如密码，身份证，等等)
		supplier.setLoginpass("");
		jsonObject.put("data", supplier);
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		return jsonObject.toString();
		
	}
	
	//获取token
	@RequestMapping(value = "/app/supplier/token")
	@ResponseBody
	public String app_supplier_token(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();
		String token="";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		Supplier supplier=supplierService.selectOne(new EntityWrapper<Supplier>().eq("token", token));
		if(supplier==null){
			jsonObject.put("msg", "账号或者密码错误");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		supplier.setLoginpass("");
		jsonObject.put("data", supplier);
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		return jsonObject.toString();
		
	}
	
	//进入个人中心
	@RequestMapping(value={"/app/supplier/mine"})
	@ResponseBody
	public String app_supplier_mine(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();
		String token="";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}		

		Supplier supplier = supplierService.selectOne(new EntityWrapper<Supplier>().eq("token", token));
		if (supplier==null) {jsonObject.put("msg", "请先登录");jsonObject.put("error", "1");return jsonObject.toString();}
		EntityWrapper<Supplier> ew = new EntityWrapper<>();
		supplier = supplierService.selectOne(ew.eq("token", token));
		jsonObject.put("data", supplier);
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		
		return jsonObject.toString();
	}
	
	//商户密码修改
	@RequestMapping(value={"/app/supplier/loginpass/up"})
	@ResponseBody
	public String app_supplier_loginpass_up(HttpServletRequest request){
		JSONObject jsonObject = new JSONObject();
		String token="";
		String yloginpass="";
		String nloginpass="";
		String nloginpass2="";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}		
		if(UtsReg.getStringPass(request.getParameter("yloginpass"))){yloginpass=request.getParameter("yloginpass").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "原密码 格式错误");return jsonObject.toString();}
		if(UtsReg.getStringPass(request.getParameter("nloginpass"))){nloginpass=request.getParameter("nloginpass").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "新密码 格式错误");return jsonObject.toString();}
		if(UtsReg.getStringPass(request.getParameter("nloginpass2"))){nloginpass2=request.getParameter("nloginpass2").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "重复密码 格式错误");return jsonObject.toString();}
		
		if(!nloginpass.equals(nloginpass2)){jsonObject.put("error", "1");jsonObject.put("msg", "两次密码不一致");return jsonObject.toString();}
		
		Supplier supplier = supplierService.selectOne(new EntityWrapper<Supplier>().eq("token", token));
		if(supplier==null){
			jsonObject.put("error", "1");jsonObject.put("msg", "请重新登陆");return jsonObject.toString();
		}
		
		if(!MD5Hash.getMd5HashPasswod(yloginpass).equals(supplier.getLoginpass())){
			jsonObject.put("error", "1");jsonObject.put("msg", "原始密码 错误");return jsonObject.toString();
		}
		
		supplier.setLoginpass(MD5Hash.getMd5HashPasswod(nloginpass2));
		
		if(!supplierService.updateById(supplier)){
			jsonObject.put("error", "1");jsonObject.put("msg", "失败");return jsonObject.toString();
		}
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();
	}
	
	//商户头像修改
	@RequestMapping(value={"/app/supplier/img/up"})
	@ResponseBody
	public String app_supplier_img_up(HttpServletRequest request){
		JSONObject jsonObject = new JSONObject();
		String token="";
		String img="";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}		
		if(UtsReg.getImg(request.getParameter("img"))){img=request.getParameter("img").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "头像 格式错误");return jsonObject.toString();}
		Supplier supplier = supplierService.selectOne(new EntityWrapper<Supplier>().eq("token", token));
		if(supplier==null){
			jsonObject.put("error", "1");jsonObject.put("msg", "请重新登陆");return jsonObject.toString();
		}
		supplier.setImg(img);
		if(!supplierService.updateById(supplier)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();
	}
	
	//商户名字修改
	@RequestMapping(value={"/app/supplier/name/up"})
	@ResponseBody
	public String app_supplier_name_up(HttpServletRequest request){
		JSONObject jsonObject = new JSONObject();
		String token="";
		String name="";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}		
		if(UtsReg.getStringZname(request.getParameter("name"))){name=request.getParameter("name").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "名字格式错误");return jsonObject.toString();}
		Supplier supplier = supplierService.selectOne(new EntityWrapper<Supplier>().eq("token", token));
		if(supplier==null){
			jsonObject.put("error", "1");jsonObject.put("msg", "请重新登陆");return jsonObject.toString();
		}
		supplier.setName(name);
		if(!supplierService.updateById(supplier)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();
	}
	
	//商户昵称修改
	@RequestMapping(value={"/app/supplier/nicheng/up"})
	@ResponseBody
	public String app_supplier_nicheng_up(HttpServletRequest request){
		JSONObject jsonObject = new JSONObject();
		String token="";
		String nicheng="";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}		
		if(UtsReg.getStringKB(request.getParameter("nicheng"))){nicheng=request.getParameter("nicheng").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "昵称 格式错误");return jsonObject.toString();}
		Supplier supplier = supplierService.selectOne(new EntityWrapper<Supplier>().eq("token", token));
		if(supplier==null){
			jsonObject.put("error", "1");jsonObject.put("msg", "请重新登陆");return jsonObject.toString();
		}
		supplier.setNicheng(nicheng);
		if(!supplierService.updateById(supplier)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();
	}
	
	//商户年龄修改
	@RequestMapping(value={"/app/supplier/age/up"})
	@ResponseBody
	public String app_supplier_age_up(HttpServletRequest request){
		JSONObject jsonObject = new JSONObject();
		String token="";
		String age="";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}		
		if(UtsReg.getIntXl(request.getParameter("age"))){age=request.getParameter("age").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "年龄 格式错误");return jsonObject.toString();}
		Supplier supplier = supplierService.selectOne(new EntityWrapper<Supplier>().eq("token", token));
		if(supplier==null){
			jsonObject.put("error", "1");jsonObject.put("msg", "请重新登陆");return jsonObject.toString();
		}
		supplier.setAge(age);
		if(!supplierService.updateById(supplier)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();
	}
	
	//商户性别修改
	@RequestMapping(value={"/app/supplier/sex/up"})
	@ResponseBody
	public String app_supplier_sex_up(HttpServletRequest request){
		JSONObject jsonObject = new JSONObject();
		String token="";
		String sex="";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}		
		if(UtsReg.getSex(request.getParameter("sex"))){sex=request.getParameter("sex").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "性别 格式错误");return jsonObject.toString();}
		Supplier supplier = supplierService.selectOne(new EntityWrapper<Supplier>().eq("token", token));
		if(supplier==null){
			jsonObject.put("error", "1");jsonObject.put("msg", "请重新登陆");return jsonObject.toString();
		}
		if ("男".equals(sex)) {
			supplier.setSex(sex);
		}else if("女".equals(sex)){
			supplier.setSex(sex);
		}else {
			jsonObject.put("msg", "只能输入男/女");jsonObject.put("error", "1");return jsonObject.toString();
		}
		if(!supplierService.updateById(supplier)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();
	}
	
	//商户邮箱修改
	@RequestMapping(value={"/app/supplier/email/up"})
	@ResponseBody
	public String app_supplier_email_up(HttpServletRequest request){
		JSONObject jsonObject = new JSONObject();
		String token="";
		String email="";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}		
		if(UtsReg.getStringEmail(request.getParameter("email"))){email=request.getParameter("email").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "邮箱 格式错误");return jsonObject.toString();}
		Supplier supplier = supplierService.selectOne(new EntityWrapper<Supplier>().eq("token", token));
		if(supplier==null){
			jsonObject.put("error", "1");jsonObject.put("msg", "请重新登陆");return jsonObject.toString();
		}
		supplier.setEmail(email);
		if(!supplierService.updateById(supplier)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();
	}
	
	//商户密码找回
	@RequestMapping(value={"/app/supplier/psw/find"})
	@ResponseBody
	public String app_supplier_psw_find(HttpServletRequest request){
		JSONObject jsonObject = new JSONObject();
		String loginname="";
		String nloginpass="";
		String nloginpass2="";
		if(UtsReg.getIsPhone(request.getParameter("loginname"))){loginname = request.getParameter("loginname").trim();}else{jsonObject.put("msg", "用户名 错误");jsonObject.put("error", "1");return jsonObject.toString();}		
		if(UtsReg.getStringPass(request.getParameter("nloginpass"))){nloginpass=request.getParameter("nloginpass").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "新密码 格式错误");return jsonObject.toString();}
		if(UtsReg.getStringPass(request.getParameter("nloginpass2"))){nloginpass2=request.getParameter("nloginpass2").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "重复密码 格式错误");return jsonObject.toString();}
		
		Supplier supplier = supplierService.selectOne(new EntityWrapper<Supplier>().eq("loginname", loginname));
		if(supplier==null){
			jsonObject.put("error", "1");jsonObject.put("msg", "该用户不存在");return jsonObject.toString();
		}
		if(!nloginpass.equals(nloginpass2)){jsonObject.put("error", "1");jsonObject.put("msg", "两次密码不一致");return jsonObject.toString();}
		
		
		supplier.setLoginpass(MD5Hash.getMd5HashPasswod(nloginpass2));
		
		if(!supplierService.updateById(supplier)){
			jsonObject.put("error", "1");jsonObject.put("msg", "失败");return jsonObject.toString();
		}
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();
	}
	
	  
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
